package quicklunch.e1.goodies.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;

public abstract class StringUtils {

    public static final char WINDOWS_SEPARATOR = '\\';
    public static final char SEPARATOR = '/';
    public static final int BUFFER_SIZE = 8192;

    /**
     * Extract the filename. e.g. aaa/bbb.txt ==> bbb.txt, / ==> empty string
     * 
     * @param given
     *            the path
     * @return extracted filename.
     */
    public static String getFilename(String s) {
        int pos = s.lastIndexOf(SEPARATOR);
        if (pos != -1) {
            return s.substring(pos + 1);
        }

        return s;
    }

    /**
     * Replace occurrences into a string.
     * 
     * @param data
     *            the string to replace occurrences into
     * @param oldS
     *            the occurrence to replace.
     * @param newS
     *            the occurrence to be used as a replacement.
     * @return the new string with replaced occurrences.
     */
    public static String replace(String data, String oldS, String newS) {
        if (data == null)
            return null;

        if (oldS == null || newS == null)
            return data;

        StringBuffer buf = new StringBuffer(data.length());
        int pos = -1;
        int i = 0;
        while ((pos = data.indexOf(oldS, i)) != -1) {
            buf.append(data.substring(i, pos)).append(newS);
            i = pos + oldS.length();
        }
        buf.append(data.substring(i));
        return buf.toString();
    }

    public static String replace(String data, char oldS, char newS) {
        if (data == null)
            return null;

        StringBuffer buf = new StringBuffer(data.length());
        int pos = -1;
        int i = 0;
        while ((pos = data.indexOf(oldS, i)) != -1) {
            buf.append(data.substring(i, pos)).append(newS);
            i = pos + 1;
        }
        buf.append(data.substring(i));
        return buf.toString();
    }

    /**
     * @param s
     * @return
     */
    public static boolean hasLength(String s) {
        return (s != null && s.length() > 0);
    }

    /**
     * @param s
     * @return
     */
    public static boolean isEmpty(String s) {
        return hasLength(s) ? false : true;
    }

    /**
     * change windows path to /
     */
    public static String cleanPath(String path) {
        return replace(path, WINDOWS_SEPARATOR, SEPARATOR);
    }

    /**
     * @param s
     * @return
     */
    public static String decapitalize(String s) {
        if (isEmpty(s))
            return s;
        char chars[] = s.toCharArray();
        chars[0] = Character.toLowerCase(chars[0]);
        return new String(chars);
    }

    /**
     * 
     */
    public static String[] tokenizeToStringArray(String str, String delimiters) {
        return tokenizeToStringArray(str, delimiters, true, true);
    }

    /**
     */
    public static String[] tokenizeToStringArray(String str, String delimiters, boolean trimTokens,
            boolean ignoreEmptyTokens) {

        StringTokenizer st = new StringTokenizer(str, delimiters);
        List tokens = new ArrayList();
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            if (trimTokens) {
                token = token.trim();
            }
            if (!ignoreEmptyTokens || token.length() > 0) {
                tokens.add(token);
            }
        }
        return (String[]) tokens.toArray(new String[tokens.size()]);
    }

    public static boolean checkEqualAsDecaptalizedString(String s, String s2) {
        return decapitalize(s).equals(decapitalize(s2));
    }

    /**
     * @param len
     * @param paddingChar
     * @return
     */
    public static String createPaddedString(int len, char paddingChar) {
        char[] c = new char[len];
        Arrays.fill(c, paddingChar);
        return new String(c);
    }

    public static int countMatches(String target, String countedStr) {
        return org.apache.commons.lang.StringUtils.countMatches(target, countedStr);
    }

    static public String getLimitsString(String s, int size) {

        byte[] b = s.getBytes();
        if (size < b.length) {
            byte[] b2 = new byte[size];
            System.arraycopy(b, 0, b2, 0, b2.length);
            s = new String(b2);
        }
        return s;
    }
     
   static public boolean mathches(String regex, String target){
	   if(target != null) return target.matches(regex);
	   return false;
   }
 
}
